<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>部门管理</el-breadcrumb-item>
    </el-breadcrumb>
  <el-card class="box-card">
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input type="text" v-model="queryInfo.name" placeholder="请输入部门名称">
          <el-button slot="append" icon="el-icon-search" @click="getDepartList()"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="adddialogFormVisible = true">添加部门</el-button>
        <el-button @click="deletelist(sels)" icon="el-icon-delete"  :disabled="this.sels.length === 0">批量</el-button>
      </el-col>
    </el-row>
      <el-table :data="DepartList" style="width: 100%" stripe @selection-change="selsChange">
        <el-table-column type="selection"  label="全选" width="55" ></el-table-column>
        <el-table-column prop="id" label="ID" width="60"></el-table-column>
        <el-table-column prop="name" label="部门名" width="120"></el-table-column>
        <el-table-column prop="total" label="人数" sortable width="80">
          <template slot-scope="scope">
            <el-tag effect="plain" v-text="scope.row.total+'人'" size="mini"></el-tag>
          </template>
        </el-table-column>
      <el-table-column prop="phone" label="办公电话" width="140"></el-table-column>
      <el-table-column prop="ctime" label="创建时间" width="230"></el-table-column>
      <el-table-column prop="optime" label="修改时间" width="230"></el-table-column>
      <el-table-column prop="address" label="地址" width="230"></el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="primary" @click="updateDepart(scope.row.id)" icon="el-icon-delete" size="small">编辑</el-button>
          <el-button type="danger" @click="deleteDepart(scope.row.id)" class="el-icon-delete" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.page"
      :page-sizes="[5, 10, 15, 30]"
      :page-size="queryInfo.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog title="添加部门" :visible.sync="adddialogFormVisible">
      <el-form :model="form" :rules="addrules" ref="ruleFormAdd">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="办公电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="部门地址" prop="address">
          <el-input type="textarea" v-model="form.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="adddialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser()" :disabled="btnDisabled" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
<!--修改-->
    <el-dialog title="修改部门" :visible.sync="editdialogFormVisible">
      <el-form :model="editRuleForm" :rules="addrules" ref="ruleFormedit">
        <el-form-item label="部门名称" prop="name">
          <el-input prop="name" v-model="editRuleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="办公电话" prop="phone">
          <el-input prop="phone" v-model="editRuleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="部门地址" prop="address">
          <el-input prop="address" type="textarea" v-model="editRuleForm.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editdialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="upDepart()" :disabled="btnDisabled" :loading="btnLoading">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  methods: {
    async getDepartList () {
      const { data: res } = await this.$http.get('sys/department', { params: this.queryInfo })
      if (res.code !== 200) {
        return this.$message.error(res.msg)
      }
      this.DepartList = res.data.data
      this.total = res.data.total
    },
    handleSizeChange (val) {
      this.queryInfo.size = val
      this.getDepartList()
    },
    handleCurrentChange (val) {
      this.queryInfo.page = val
      this.getDepartList()
    },
    addUser () { // 添加
      this.$refs.ruleFormAdd.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post('sys/department/add', this.form)
          if (res.code !== 200) {
            this.$message.error(res.msg)
            return false
          }
          this.$message.success(res.msg)
          await this.getDepartList()
          this.adddialogFormVisible = false
        }
      })
    },
    deleteDepart (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete('sys/department/delete/' + id)
        if (res.code !== 200) {
          return this.$message.error(res.msg)
        }
        await this.getDepartList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    deletelist () {
      var ids = this.sels.map(item => item.id).join()
      // var id = (this.$refs.multipleTable.selection).map(item => item.id).join()
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete('sys/loginlog/deleteList/' + ids)
        if (res.code !== 200) {
          return this.$message.error(res.msg)
        }
        await this.getLogList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    selsChange (sels) {
      this.sels = sels
    },
    async updateDepart (id) { // 展示部门信息
      const { data: res } = await this.$http.get('sys/department/update/' + id)
      if (res.code === 200) {
        this.editRuleForm = res.data
      } else {
        return this.$message.error('部门信息编辑失败' + res.msg)
      }
      this.editdialogFormVisible = true
    },
    async upDepart () {
      this.$confirm('此操作将修改该部门信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.put('sys/department/findDeanList/' + this.editRuleForm.id, this.editRuleForm)
        if (res.code !== 200) {
          return this.$message.error(res.msg)
        }
        this.editdialogFormVisible = false
        await this.getDepartList()
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        })
      })
    }
  },
  async getDeanList () {
    const { data: res } = await this.$http.get('department/findDeanList')
    if (res.code !== 200) {
      return this.$message.error('获取部门主任失败')
    } else {
      this.deans = res.data
    }
  },
  mounted () {
    this.getDepartList()
  },
  name: 'Depart',
  data () {
    // eslint-disable-next-line no-unused-vars
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regMobile.test(value)) {
        return cb()
      }

      cb(new Error('请输入合法的手机号'))
    }
    return {
      sels: [],
      btnDisabled: false,
      btnLoading: false,
      DepartList: [],
      total: 0,
      queryInfo: {
        name: '',
        page: 1,
        size: 10
      },
      adddialogFormVisible: false,
      editdialogFormVisible: false,
      form: {
        name: '',
        mgr_id: '',
        phone: '',
        address: ''
      },
      editRuleForm: {
        name: '',
        mgr_id: '',
        phone: '',
        address: ''
      },
      shuru: {
        name: ''
      },
      formLabelWidth: '120px',
      addrules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        mgr_id: [
          { required: true, message: '请选择部门主任', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入部门地址', trigger: 'blur' }
        ]
      }
    }
  }
}
</script>

<style scoped>

</style>
